//
//  MoreViewController.m
//  安好
//
//  Created by lianchuang on 14-11-10.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "MoreViewController.h"
#import "MoreCell.h"
#import "SetViewController.h"
#import "TaskViewController.h"
#import "EncyViewController.h"
#import "NearbyViewController.h"
#import "RegistrationViewController.h"
#import "SelfInspectionViewController.h"
#import "TiJianRootViewController.h"
#import "LoginViewController.h"
#import "MyHealthLogViewController.h"

#define MoreCellLineColor [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1]
#define IPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

@interface MoreViewController ()<UITableViewDelegate,UITableViewDataSource>
@end

@implementation MoreViewController

#pragma mark - 将要显示的时 取出NSUserDefaults中得数据 刷新table
-(void)viewWillAppear:(BOOL)animated
{
    //取出数据
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    self.haveCellArray = [user objectForKey:@"MoreSetSwitch"];
    //刷新table（主要是刷新各个cell的高度）
    [self.tableView reloadData];
}
#pragma mark ----
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self makeNavBar];
    [self loadData];
    [self makeTableView];
}


#pragma mark - 导航条的设置
-(void)makeNavBar
{
    self.navigationItem.title = @"发现";
    UIBarButtonItem *setBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_设置.png"] style:UIBarButtonItemStylePlain target:self action:@selector(pushSetVC)];
    [setBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.rightBarButtonItem = setBtnImg;
}
/**
 *  触发"设置" - push到set页面
 *     push之前传值
 */
-(void)pushSetVC
{
    SetViewController *setVC = [[SetViewController alloc] init];
    //把当前的cell存在情况haveCellArray传给，set页面的switch状态数组
    setVC.switchStatesArray = (NSMutableArray *)self.haveCellArray;
        
    [self.navigationController pushViewController:setVC animated:YES];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (self.navigationController.viewControllers.count == 1)//关闭主界面的右滑返回
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

#pragma mark - 加载数据、 从文件NSUserDefaults中读取数据
-(void)loadData
{
    self.moreArray = [NSMutableArray arrayWithCapacity:0];
    //从本地NSUserDefaults读取数据,得到前4个cell的存在状态
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    self.haveCellArray = [user objectForKey:@"MoreSetSwitch"];
    /**
     * 如果是第一次运行,NSUserDefaults 中没有存入数据。
     * 需要创建初始数据 存入本低
     */
    if (self.haveCellArray.count == 0) {
        self.haveCellArray = @[@"1",@"1",@"1",@"1"];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:self.haveCellArray forKey:@"MoreSetSwitch"];
        [user synchronize];
    }
    //创建cell的数据
    NSArray *titleArray = @[@"日志",@"任务",@"百科",@"附近",@"体检",@"挂号",@"自查"];
    NSArray *detialArray = @[@"记录身体各项指数",@"你的健康提醒工具 ",@"你的随身健康百科",@"即时就近就医购药",@"便捷健康体验工具",@"权威医院实时号源",@"身体症状快速查询"];
    //发现_任务@2x.png
    NSMutableArray *imgArray = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < titleArray.count; i++) {
            [imgArray addObject:[NSString stringWithFormat:@"发现_%@.png",titleArray[i]]];
    }
    /**
     * 把数据做成一个个字典(modal)存入moreArray中
     *
     */
    for (NSInteger i = 0; i < titleArray.count; i++) {
        NSDictionary *dic = @{@"titleName":titleArray[i],@"detial":detialArray[i],@"imgName":imgArray[i]};
        [self.moreArray addObject:dic];
    }
}

#pragma  mark - 创建tableView 设置代理、添加header视图
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin-TABBAR_HEIGHT+1) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView setSeparatorColor:MoreCellLineColor];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        if (IPhone6Plus) {
           [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 85, 0, 0)];
        }else{
            [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 70, 0, 0)];
        }
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        if (IPhone6Plus) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 85, 0, 0)];
        }else{
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 70, 0, 0)];
        }
    }
    
}
#pragma - mark tableView的代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //根据haveCellArray的值确定 cell的高（只对前4行处理）
    if (indexPath.row < 4 && [self.haveCellArray[indexPath.row] intValue]== 0)
    {
        return 0;
    }
    else
    {
        if (IPhone6Plus) {
            return 78;
        }else{
            return 64;
        }
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.moreArray.count;
}
#pragma mark ---- 加载cell
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"moreCell"];
    if (cell == nil) {
        if (IPhone6Plus) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MoreCell" owner:self options:nil] lastObject];
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MoreCell" owner:self options:nil] firstObject];
        }
        
    }
    /**
     *  根据传过来的haveCellArray确定cell上面的控件是否隐藏
     *  和对应行的行高对应，隐藏行的行高是0
     */
    if (indexPath.row < 4 && [self.haveCellArray[indexPath.row] intValue]== 0)
    {
        cell.contentView.hidden = YES;
    }else
    {
        cell.contentView.hidden = NO;
    }
    /**
     *  对cell加载数据
     */
    [cell.imgeView setImage:[UIImage imageNamed:[self.moreArray[indexPath.row] objectForKey:@"imgName"]]];
    cell.imgeView.clipsToBounds = YES;
    cell.imgeView.layer.cornerRadius = 3;
    cell.titleLable.text = [self.moreArray[indexPath.row] objectForKey:@"titleName"];
    cell.detialLable.text = [self.moreArray[indexPath.row] objectForKey:@"detial"];
    return cell;
}
#pragma mark ---- 选中某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:{
            MyNSLog("健康日志");
            // “健康日志”
            MyHealthLogViewController *myHealthLogVC = [[MyHealthLogViewController alloc] init];
            [self.navigationController pushViewController:myHealthLogVC animated:YES];
            break;
        }
        case 1:{
            // “任务”
            TaskViewController *task = [[TaskViewController alloc] init];
            [self.navigationController pushViewController:task animated:YES];
            break;
        }
        case 2:{
            // “百科”
            EncyViewController *ency = [[EncyViewController alloc] init];
            [self.navigationController pushViewController:ency animated:YES];
            break;
        }
        case 3:{
            // “附近”
            NSString *string=[NSString stringWithFormat:@"%@",[[UserManager shareInstance] getUserIsLogin]];;
            if (string.length>0 ) {
                NearbyViewController *nearby = [[NearbyViewController alloc] initWithNibName:@"NearbyViewController" bundle:[NSBundle mainBundle]];
                [self.navigationController pushViewController:nearby animated:YES];
            }else
            {
                [self isLogin];
            }
            break;
        }
        case 4:{
            //体检
            TiJianRootViewController *tijianRoot = [[TiJianRootViewController alloc]init];
            [self.navigationController pushViewController:tijianRoot animated:YES];
            break;
        }
            
        case 5:{
            // “挂号”
            RegistrationViewController *registration = [[RegistrationViewController alloc] init];
            [self.navigationController pushViewController:registration animated:YES];
            break;
        }
        case 6:{
            // “自查”
            SelfInspectionViewController *selfInspection = [[SelfInspectionViewController alloc] init];
            [self.navigationController pushViewController:selfInspection animated:YES];
            break;
        }
        default:
            break;
    }
}
@end
